const config = require('../../common/config');
const util = require('../../utils/util');

Page({
    data: {
        icon_add: config.WEB_ROOT + "/images/icons/add_data.png",
        userId: '',
        shopList: []
    },
    onLoad: function () {
        wx.setNavigationBarTitle({
            title: '我的店铺'
        })
        this.setData({
            userId: wx.getStorageSync(config.USER_ID)
        })
        this.getMyShopList();
    },
    getMyShopList: function () {
        var _this = this;
        wx.showLoading({
            title: '数据加载..',
            icon: 'loading',
            duration: config.TIME_OUT,
            mask: true
        })

        wx.request({
            url: config.COMMPT_REQPATH,
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method: 'POST',
            data: {
                accessToken: '',
                refreshToken: '',
                serviceName: 'wxShopManageService',
                methodName: 'getShopListByUserId',
                reqData: JSON.stringify({
                    userId: _this.data.userId
                })
            },
            success: function (res) {
                var resultData = res.data;
                if (resultData.code == 1) {
                    _this.setData({
                        shopList: resultData.data.bzShopList
                    })
                } else {
                    util.debug('mystorejs：获取我的店铺列表失败');
                    util.debug(resultData);
                }
            },
            fail: function (e) {
                util.debug(e.errMsg);
            },
            complete: function () {
                wx.hideLoading();
                _this.setData({
                    isLoading: false
                })
            }
        })
    },
    showManageList: function (e) {
        var that = this;
        var shopId = e.currentTarget.dataset.shopid;
        var shopStatus = e.currentTarget.dataset.status;
        var itemList = [];
        if (shopStatus == '0' || shopStatus == '3') {
            //已下线&审核通过:sx|xg|sc
            itemList = ['上线', '修改', '删除'];
        } else if (shopStatus == '1') {
            //正常:xx
            itemList = ['下线'];
        } else {
            //未审核&审核未通过&状态未知:xg|sc
            itemList = ['修改', '删除'];
        }

        wx.showActionSheet({
            itemList: itemList,
            itemColor: '#777',
            success: function (res) {
                if (res.tapIndex == 0) {
                    if (shopStatus == '0' || shopStatus == '3') {
                        that.operationShop(shopId, 'sx');
                    } else if (shopStatus == '1') {
                        that.operationShop(shopId, 'xx');
                    } else {
                        that.operationShop(shopId, 'xg');
                    }
                }
                if (res.tapIndex == 1) {
                    if (shopStatus == '0' || shopStatus == '3') {
                        that.operationShop(shopId, 'xg');
                    } else {
                        that.operationShop(shopId, 'sc');
                    }
                }
                if (res.tapIndex == 2) {
                    that.operationShop(shopId, 'sc');
                }
            },
            fail: function (res) {
                util.debug("mystorejs:" + res.errMsg)
            }
        })
    },
    operationShop: function (shopId, status) {
        var that = this;
        util.debug(shopId + "|" + status);
        if (status == 'xg') {
            //转修改界面
            wx.navigateTo({
                url: '../../pages/shopmodify/index?shopId='+shopId
            })
        } else {
            let shopStatus = '';
            if (status == 'sx') {
                //修改为上线状态
                shopStatus = '1';

            } else if (status == 'xx') {
                //修改为下线状态
                shopStatus = '0';

            } else if (status == 'sc') {
                //删除
                shopStatus = '-1';
            }

            wx.showLoading({
                title: '加载中..',
                icon: 'loading',
                duration: config.TIME_OUT,
                mask: true
            })

            wx.request({
                url: config.COMMPT_REQPATH,
                header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                method: 'POST',
                data: {
                    accessToken: '',
                    refreshToken: '',
                    serviceName: 'wxShopManageService',
                    methodName: 'setShopStatus',
                    reqData: JSON.stringify({
                        userId: that.data.userId,
                        shopId:shopId,
                        shopStatus:shopStatus
                    })
                },
                success: function (res) {
                    var resultData = res.data;
                    util.debug(resultData);
                    if (resultData.code == 1) {
                        wx.redirectTo({
                            url: '../../pages/mystore/index'
                        })
                    }
                },
                fail: function (e) {
                    util.debug(e.errMsg);
                },
                complete: function () {
                    wx.hideLoading();
                }
            })
        }
    }
});